var app = getApp(), api = app.api;

Page({
    data: {
        is_new_user: false,
        team_order_money: 0,
        msgList: [],
        achievementprice: '0.00',
        total_price: 0,
        price: 0,
        subcompany_price: 0,
        cash_price: 0,
        total_cash: 0,
        subcompany_cach_price: 0,
        team_count: 0,
        team_count_tuike: 0,
        order_money: 0,
        subcompany_order_money: 0,
        hiddenmodalput: true,
        tuikeid: '',
        xj: '',
        jlxj: '一星',
        jlsl: 0,
        jlsls: 0,
        first: 0
    },
    hehuoren(e) {
        if (this.data.__user_info.is_manager == 0) {
            wx.navigateTo({
                url: '/pages/tuibao-integral-mall/manage/manage',
            })
        }
        else {
            wx.showToast({
                title: '您已成为' + this.data.custom.words.share_manger_name.name + '无需再次申请',
            })
        }
    },
    onLoad: function (e) {
        getApp().page.onLoad(this, e);
        this.setData({
            custom: getApp().core.getStorageSync(getApp().const.CUSTOM)
        });
        this.text_withdraw();


    },
    user_status: function (e) {
        var o = this
        getApp().request({
            url: getApp().api.default.user_status,
            success: function (e) {
                o.setData({
                    is_new_user: e.data.is_new_user
                })
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    team_shop_money: function (e) {
        var o = this
        getApp().request({
            url: getApp().api.share.team_shop_money,
            success: function (res) {
                if (res.code == 0) {
                    o.setData({
                        team_order_money: res.data.team_order_money
                    })
                }

            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    purchase: function (e) {
        wx.navigateTo({
            url: '/purchase/index/index',
        })
    },
    achievement: function (e) {
        wx.navigateTo({
            url: '/pages/achievement/achievement',
        })
    },
    Nineactivities: function (e) {
        wx.navigateTo({
            url: '/pages/Nineactivities/Nineactivities',
        })
    },
    gettime: function (e) {
        var a = this
        getApp().request({
            url: getApp().api.share.get_team,
            data: {
                status: 1,
                page: 1,
                keyword: ''
            },
            success: function (t) {
                console.log(t)
                if (t.code == 0) {
                    a.setData({
                        first: t.data.first
                    })
                }

            },
            complete: function () {

            }
        })
    },
    text_withdraw: function () {
        var o = this
        getApp().request({
            url: getApp().api.default.text_withdraw,
            success: function (e) {
                if (e.code == 0) {
                    o.setData({
                        msgList: e.data.list
                    })
                }
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    onReady: function () {
        getApp().page.onReady(this);

    },
    onShow: function () {
        getApp().page.onShow(this);
        var e = this, t = getApp().core.getStorageSync(getApp().const.SHARE_SETTING), a = e.data.__user_info;
        e.gettime(), e.user_status(), e.team_shop_money();
        if (a.is_subcompany == 1) {
            var timestamp = Date.parse(new Date());
            var date = new Date(timestamp);
            //获取年份  
            var Y = date.getFullYear();
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
            console.log("当前时间：" + Y + '年' + M + '月');
            getApp().request({
                url: getApp().api.share.achievement_list,
                data: {
                    page: 1,
                    month: Y + '-' + M
                },
                success: function (c) {
                    console.log(c)
                    e.setData({
                        achievementprice: c.data.list[0].price
                    })
                }
            })
        }
        e.setData({
            share_setting: t,
            custom: e.data.store.share_custom_data
        }), a && 1 == a.is_distributor ? e.checkUser() : e.loadData();
        setTimeout(function () {
            getApp().core.setNavigationBarColor({
                backgroundColor: "#463B65",
                bottomBackgroundColor: "#ffffff",
                frontColor: "#ffffff",
            })
        }, 500)
    },
    checkUser: function () {
        var t = this;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.share.get_info,
            success: function (e) {

                0 == e.code && (
                    console.log('000', e.data.team_count_tuike),
                    0 <= e.data.team_count_tuike && e.data.team_count_tuike < 100 && (
                        console.log('1', 100 - e.data.team_count_tuike),
                        t.setData({
                            xj: '',
                            jlxj: '一星',
                            jlsl: 100 - e.data.team_count_tuike,
                            jlsls: e.data.team_count_tuike
                        })
                    ),
                    100 <= e.data.team_count_tuike && e.data.team_count_tuike < 200 && (
                        console.log('2', 200 - e.data.team_count_tuike),
                        t.setData({
                            xj: '一星',
                            jlxj: '二星',
                            jlsl: 200 - e.data.team_count_tuike,
                            jlsls: e.data.team_count_tuike - 100
                        })
                    ),
                    200 <= e.data.team_count_tuike && e.data.team_count_tuike < 300 && (
                        console.log('3', 200 - e.data.team_count_tuike),
                        t.setData({
                            xj: '二星',
                            jlxj: '三星',
                            jlsl: 300 - e.data.team_count_tuike,
                            jlsls: e.data.team_count_tuike - 200
                        })
                    ),
                    300 <= e.data.team_count_tuike && e.data.team_count_tuike < 400 && (
                        console.log('3', 200 - e.data.team_count_tuike),
                        t.setData({
                            xj: '三星',
                            jlxj: '四星',
                            jlsl: 400 - e.data.team_count_tuike,
                            jlsls: e.data.team_count_tuike - 300
                        })
                    ),
                    400 <= e.data.team_count_tuike && e.data.team_count_tuike < 500 && (
                        console.log('3', 200 - e.data.team_count_tuike),
                        t.setData({
                            xj: '四星',
                            jlxj: '五星',
                            jlsl: 500 - e.data.team_count_tuike,
                            jlsls: e.data.team_count_tuike - 400
                        })
                    ),
                    e.data.team_count_tuike >= 500 && (
                        console.log('3', 200 - e.data.team_count_tuike),
                        t.setData({
                            xj: '五星',
                            jlxj: '',
                            jlsl: '',
                            jlsls: 100
                        })
                    ),
                    t.setData({
                        total_price: e.data.price.total_price,
                        price: e.data.price.price,
                        subcompany_price: e.data.price.subcompany_price,
                        cash_price: e.data.price.cash_price,
                        total_cash: e.data.price.total_cash,
                        subcompany_cach_price: e.data.price.subcompany_cach_price,
                        team_count: e.data.team_count,
                        team_count_tuike: e.data.team_count_tuike,
                        order_money: e.data.order_money,
                        subcompany_order_money: e.data.subcompany_order_money,
                        custom: e.data.custom,
                        order_money_un: e.data.order_money_un
                    }), getApp().core.setStorageSync(getApp().const.CUSTOM, e.data.custom), t.loadData()),
                    1 == e.code && (__user_info.is_distributor = e.data.is_distributor, t.setData({
                        __user_info: __user_info
                    }), getApp().setUser(__user_info));
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    loadData: function () {
        var a = this;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.share.index,
            success: function (e) {
                if (0 == e.code) {
                    if (e.data.share_setting) var t = e.data.share_setting; else t = e.data;
                    getApp().core.setStorageSync(getApp().const.SHARE_SETTING, t), a.setData({
                        share_setting: t
                    });
                }
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    onHide: function () {
        getApp().page.onHide(this);
    },
    onUnload: function () {
        getApp().page.onUnload(this);
    },
    onPullDownRefresh: function () { },
    onReachBottom: function () { },
    apply: function (t) {
        var a = getApp().core.getStorageSync(getApp().const.SHARE_SETTING), o = getApp().getUser();
        1 == a.share_condition ? getApp().core.navigateTo({
            url: "/pages/add-share/index"
        }) : 0 != a.share_condition && 2 != a.share_condition || (0 == o.is_distributor ? getApp().core.showModal({
            title: "申请成为" + (this.data.custom.words.share_name.name || "分销商"),
            content: "是否申请？",
            success: function (e) {
                e.confirm && (getApp().core.showLoading({
                    title: "正在加载",
                    mask: !0
                }), getApp().request({
                    url: getApp().api.share.join,
                    method: "POST",
                    data: {
                        form_id: t.detail.formId
                    },
                    success: function (e) {
                        0 == e.code && (0 == a.share_condition ? (o.is_distributor = 2, getApp().core.navigateTo({
                            url: "/pages/add-share/index"
                        })) : (o.is_distributor = 1, getApp().core.redirectTo({
                            url: "/pages/share/index"
                        })), getApp().setUser(o));
                    },
                    complete: function () {
                        getApp().core.hideLoading();
                    }
                }));
            }
        }) : getApp().core.navigateTo({
            url: "/pages/add-share/index"
        }));
    },

    kanjia: function () {
        this.setData({
            hiddenmodalput: !this.data.hiddenmodalput,
            tuikeid: ''
        })
    },

    tuikeid: function (e) {
        var tuikeid = e.detail.value
        this.setData({
            tuikeid: tuikeid
        })
    },
    //取消按钮  
    cancel: function () {
        this.setData({
            hiddenmodalput: true
        });
    },
    //确认  
    confirm: function () {
        var that = this
        var rex = /^[0-9]+$/;//正则表达式
        var flag = rex.test(that.data.tuikeid)
        if (that.data.tuikeid != '' && flag) {
            wx.showModal({
                title: '提示',
                content: '是否分配砍价次数给ID为' + that.data.tuikeid + '的推客',
                success(res) {
                    if (res.confirm) {
                        getApp().request({
                            url: getApp().api.share.partner_add_agent,
                            method: "POST",
                            data: {
                                agent_id: that.data.tuikeid
                            },
                            success: function (e) {
                                if (e.code === 0) {
                                    wx.showToast({
                                        title: '分配成功',
                                        icon: 'success',
                                        duration: 2000
                                    })
                                    that.loadData()
                                    that.setData({
                                        hiddenmodalput: true
                                    });
                                } else {
                                    wx.showToast({
                                        title: e.msg,
                                        icon: 'none',
                                        duration: 2000
                                    })
                                }

                            },
                            complete: function () {
                                getApp().core.hideLoading();
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            wx.showToast({
                title: '请输入正确的推客ID',
                icon: 'none',
                duration: 2000
            })
        }

    },
    loadData: function (e) {
        var t = this;
        t.setData({
            store: getApp().core.getStorageSync(getApp().const.STORE)
        }), getApp().request({
            url: getApp().api.user.index,
            success: function (o) {
                if (0 == o.code) {
                    if ("my" == t.data.__platform) o.data.menus.forEach(function (e, t, a) {
                        "bangding" === e.id && o.data.menus.splice(t, 1, 0);
                    });
                    t.setData(o.data), getApp().core.setStorageSync(getApp().const.PAGES_USER_USER, o.data),
                        getApp().core.setStorageSync(getApp().const.SHARE_SETTING, o.data.share_setting),
                        getApp().core.setStorageSync(getApp().const.USER_INFO, o.data.user_info);
                }
            }
        });
    },
    cash_detail: function (e) {
        wx.navigateTo({
            url: '/pages/cash-detail/cash-detail',
        })
    },
    cash: function (e) {
        wx.navigateTo({
            url: '/pages/cash/cash',
        })
    },
    share_team: function (e) {
        console.log(e)
        var dd = e.currentTarget.dataset.dd
        if (dd == '1') {
            wx.navigateTo({
                url: '/pages/share-team/share-team?dd=' + dd,
            })
        } else {
            wx.navigateTo({
                url: '/pages/share-team/share-team',
            })
        }

    },
    unsettled: function (e) {
        wx.navigateTo({
            url: '/pages/unsettled/unsettled',
        })
    }
});